package com.ceres.device.web.controller;

import com.ceres.device.pojo.CustomerDevice;
import com.ceres.device.web.service.CustomerDeviceService;
import com.ceres.entity.Result;
import com.ceres.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/customerDevice")
public class CustomerDeviceController {

    @Autowired
    private CustomerDeviceService customerDeviceService;

    /**
     * 根据设备类型进行分类查询
     * @param deviceType
     * @return
     */
    @GetMapping("/deviceType")
    public Result findByType(@RequestParam String deviceType){
        try{
            List<CustomerDevice> customerDeviceList = customerDeviceService.findByDeviceType(deviceType);
            return new Result(true, StatusCode.OK,customerDeviceList);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR);
        }
    }

    /**
     * 根据id删除设备
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    public Result deleteById(Integer id){
        try{
            customerDeviceService.deleteById(id);
            return new Result(true, StatusCode.OK,id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR);
        }
    }

    /**
     * 修改设备基本信息
     * @param customerDevice
     * @return
     */
    @PostMapping("/updateById")
    public Result updateById(CustomerDevice customerDevice){
        try {
            customerDeviceService.updateById(customerDevice);
            return new Result(true, StatusCode.OK,customerDevice);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR);
        }
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id){
        try{
            CustomerDevice byId = customerDeviceService.findById(id);
            return new Result(true, StatusCode.OK,byId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR);
        }
    }

    /**
     * 自行采购
     * @param map
     * @return
     */
    @PostMapping(value = "/add")
    public Result addCustomerDevice(@RequestBody Map<String, Object> map){
        try {
            customerDeviceService.addCustomerDevice(map);
            return new Result(true, StatusCode.OK);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR);
        }
    }

}
